<template>
  <cloud-dialog
    title="保存提示"
    ok-text="保存"
    :width="800"
    :visible="visible"
    :before-ok="handleSubmit"
    :before-close="handleDialogClose"
    @update:visible="emit('update:visible', $event)">
    <confirm-box :diff="record" :risks="tips"></confirm-box>
  </cloud-dialog>
</template>

<script setup lang="ts">
import { updateApiNodeUpdateCascadeRegionApi } from '@/apis/modules/node'
import {
  type CascaderDiff,
  type CascaderUpdateModel,
  ConfirmBox
} from '@/components/Cascader'

interface IEmit {
  (event: 'update:visible', value: boolean): void
  (event: 'update:record', value: CascaderDiff[]): void
  (event: 'save'): void
}

interface IProps {
  visible: boolean
  record?: CascaderDiff[]
  tips?: string[]
  diffData?: CascaderUpdateModel
}

const emit = defineEmits<IEmit>()
const props = defineProps<IProps>()

async function handleSubmit(diff: CascaderDiff[]) {
  const data = (diff || props.record)?.map((item) => {
    return {
      region_id: item.to.region,
      cascade_level: item.to.level,
      id: item.node
    }
  })
  await updateApiNodeUpdateCascadeRegionApi(data)
  ToastUtils.success('保存成功')
  emit('save')
}

// dialog关闭
function handleDialogClose() {
  emit('update:record', [])
}

defineExpose({ save: handleSubmit })
</script>

<style scoped lang="less"></style>
